<?php
class ControllerCommunityCommunity extends Controller{
    public function index(){
        $this->language->load('community/community');
        $this->language->load('community/community_header');
                
        $this->document->setTitle($this->language->get('heading_title'));
                
        $this->load->model('community/community');

        //detect if user login
        if($this->customer->isLogged()){
            $this->data['is_user_login']=TRUE;
        }else{
            $this->data['is_user_login']=FALSE;
        }

        //registered user online status
        $this->data['online_user_total']=$this->model_community_community->getOnlineUserTotal()+mt_rand(400 ,500);

        if(false){
            $results=$this->model_community_community->getOnlineUsers();

            $this->data['online_users']=array();

            $o=0;
            foreach($results as $result){
                $this->data['online_users'][]=array(
                    'profile_link'           =>      $this->url->link('community/profile','uid=' . $result['user_id'] ,'SSL'),
                    'username'               =>      $result['username']
                );

                switch($result['user_type']){
                    case 1:
                        $icon_style='admin-icon';
                        break;
                    case 2:
                        $icon_style='global-mod-icon';
                        break;
                    case 3:
                        $icon_style='mod-icon';
                        break;
                    default:
                        $icon_style='member-icon';
                }

                $this->data['online_users'][$o]['icon_style']=$icon_style;

                $o++;
            }
        }

        //guest online status
        $file='system/guest_online_list.txt';
        touch($file);
        $file_record=file($file);
        $guest_online_list=array();

        foreach($file_record as $line){
            $result=explode('|',$line);

            if(trim($result[1])+$this->config->get("online_time_span")*60>=time()){
                $guest_online_list[$result[0]]=trim($result[1]);
            }
        }

        $online_guest_total=count($guest_online_list)+mt_rand(100, 200);

        $online_peak=$this->config->get('most_user_online');

        $this->data['text_online_total']=sprintf($this->language->get('text_online_total'),$this->data['online_user_total']+$online_guest_total);
        $this->data['text_online_user']=sprintf($this->language->get('text_online_user'),$this->data['online_user_total']);
        $this->data['text_online_guest']=sprintf($this->language->get('text_online_guest'),$online_guest_total);
        $this->data['text_online_peak']=sprintf($this->language->get('text_online_peak'),$online_peak[0],date($this->language->get('date_time_format_short'),$online_peak[1]));

        if($this->data['online_user_total']+$online_guest_total>$online_peak[0]){
            $this->model_community_community->UpdateOnlinePeak($this->data['online_user_total']+$online_guest_total);
        }

        $newst_member=$this->config->get('newest_member');
        if($newst_member){
            $newst_member_link=$this->url->link('community/profile','uid=' . $newst_member[0] ,'SSL');
            $newst_member_name=$newst_member[1];
            $this->data['text_newest_member']=sprintf($this->language->get('text_newest_member'),$newst_member_link,$newst_member_name);
        }else{
            $this->data['text_newest_member']=$this->language->get('text_no_newest_member');
        }

        $this->data['text_post_total']=sprintf($this->language->get('text_post_total'),$this->config->get('total_posts')+265);
        $this->data['text_topic_total']=sprintf($this->language->get('text_topic_total'),$this->config->get('total_topics')+116);
        $this->data['text_member_total']=sprintf($this->language->get('text_member_total'),$this->config->get('total_users')+5019);

        $this->data['breadcrumbs']=array();
        
        $this->data['breadcrumbs'][]=array(
            'href'      =>      $this->url->link('common/home'),
            'text'      =>      $this->language->get('text_home'),
            'separator'=>      FALSE
        );
        
        $this->data['breadcrumbs'][]=array(
            'href'      =>      $this->url->link('community/community'),
            'text'      =>      $this->language->get('text_community'),
            'separator' =>      $this->language->get('text_separator')
        );

        $this->data['text_topics']=$this->language->get('text_topics');
        $this->data['text_posts']=$this->language->get('text_posts');
        $this->data['text_latest_post']=$this->language->get('text_latest_post');
        $this->data['text_by']=$this->language->get('text_by');
        $this->data['text_never']=$this->language->get('text_never');
        $this->data['text_who_online']=$this->language->get('text_who_online');
        $this->data['text_statics']=$this->language->get('text_statics');
        $this->data['text_legend']=$this->language->get('text_legend');
        $this->data['text_admin']=$this->language->get('text_admin');
        $this->data['text_global_mod']=$this->language->get('text_global_mod');
        $this->data['text_mod']=$this->language->get('text_mod');
        $this->data['text_member']=$this->language->get('text_member');

        $this->data['forum_cats']=array();
        $this->data['forums']=array();
        $this->data['sub_forums']=array();

        $forumCats=$this->model_community_community->getForumCatInfo();

        foreach($forumCats as $forumCat){
            $this->data['forum_cats'][]=array(
                'forum_id'      =>  $forumCat['forum_id'],
                'forum_name'    =>  $forumCat['forum_name']
            );

            $forums=$this->model_community_community->getForumInfo($forumCat['forum_id']);

            foreach($forums as $forum){
                $this->data['forums'][$forumCat['forum_id']][]=array(
                    'forum_id'                  =>  $forum['forum_id'],
                    'forum_name'                =>  $forum['forum_name'],
                    'forum_desc'                =>  $forum['forum_desc'],
                    'forum_topics'              =>  $forum['forum_topics'],
                    'forum_posts'               =>  $forum['forum_posts'],
                    'user_link'                 =>  $this->url->link('community/profile','uid=' . $forum['forum_last_poster_id'] ,'SSL'),
                    'forum_last_poster_name'    =>  $forum['forum_last_poster_name'],
                    'forum_last_post_time'      =>  date($this->language->get('date_time_format_short'),$forum['forum_last_post_time']),
                    'forum_link'                =>  $this->url->link('community/viewforum','fid=' . $forum['forum_id'] ,'SSL')
                );

                //get forum track
                if(!empty($forums)){
                    if($forum['forum_posts']>0){
                        if(isset($forum['mark_time'])){
                            $this->data['forumStatus'][$forumCat['forum_id']][]=($forum['mark_time']>$forum['forum_last_post_time']) ? 'forum_read' : 'forum_unread';
                            $this->data['text_forum_status'][$forumCat['forum_id']][]=($forum['mark_time']>$forum['forum_last_post_time']) ? $this->language->get('text_forum_read') : $this->language->get('text_forum_unread');
                        }else{
                            if(!$this->customer->isLogged()){
                                $this->data['forumStatus'][$forumCat['forum_id']][]='forum_read';
                                $this->data['text_forum_status'][$forumCat['forum_id']][]=$this->language->get('text_forum_read');
                            }else{
                                $this->data['forumStatus'][$forumCat['forum_id']][]='forum_unread';
                                $this->data['text_forum_status'][$forumCat['forum_id']][]=$this->language->get('text_forum_unread');
                            }
                        }
                    }else{
                        $this->data['forumStatus'][$forumCat['forum_id']][]='forum_read';
                        $this->data['text_forum_status'][$forumCat['forum_id']][]=$this->language->get('text_forum_read');
                    }
                }

                //subforum
                $subForums=$this->model_community_community->getSubForumInfo($forumCat['forum_id'],$forum['forum_id']);

                foreach($subForums as $subForum){
                    $this->data['sub_forums'][$forumCat['forum_id']][$forum['forum_id']][]=array(
                        'forum_name'                =>  $subForum['forum_name'],
                        'forum_link'                =>  $this->url->link('community/viewforum','fid=' . $subForum['forum_id'] ,'SSL')
                    );

                    //get sub forum track
                    if(!empty($subForums)){
                        if($subForum['forum_posts']>0){
                            if(isset($subForum['mark_time'])){
                                $this->data['subforumStatus'][$forumCat['forum_id']][$forum['forum_id']][]=($subForum['mark_time']>$subForum['forum_last_post_time']) ? 'class="subforum_read"' : 'class="subforum_unread"';
                                $this->data['text_subforum_status'][$forumCat['forum_id']][$forum['forum_id']][]=($subForum['mark_time']>$subForum['forum_last_post_time']) ? $this->language->get('text_forum_read') : $this->language->get('text_forum_unread');
                            }else{
                                if(!$this->customer->isLogged()){
                                    $this->data['subforumStatus'][$forumCat['forum_id']][$forum['forum_id']][]='class="subforum_read"';
                                    $this->data['text_subforum_status'][$forumCat['forum_id']][$forum['forum_id']][]=$this->language->get('text_forum_read');
                                }else{
                                    $this->data['subforumStatus'][$forumCat['forum_id']][$forum['forum_id']][]='class="subforum_unread"';
                                    $this->data['text_subforum_status'][$forumCat['forum_id']][$forum['forum_id']][]=$this->language->get('text_forum_unread');
                                }
                            }
                        }else{
                            $this->data['subforumStatus'][$forumCat['forum_id']][$forum['forum_id']][]='class="subforum_read"';
                            $this->data['text_subforum_status'][$forumCat['forum_id']][$forum['forum_id']][]=$this->language->get('text_forum_read');
                        }
                    }
                }
            }
        }

		if(file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/community/community.tpl')){
            $this->template=$this->config->get('config_template') . '/template/community/community.tpl';
		}else{
            $this->template='default/template/community/community.tpl';
		}	
			
        $this->children = array(
            'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header',
			'community/community_header'
		);
		
		$this->response->setOutput($this->render()); 	
  	}

    public function dailyBonus(){

    }
}
?>